<template>
  <Main>
    <el-button size="small" type="primary" @click="add()" icon="el-icon-plus">新增文章</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="kid"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
     <el-table-column fixed prop="code" label="编号" ></el-table-column>
      <el-table-column fixed prop="title" label="标题" ></el-table-column>
      <el-table-column prop="file_name" label="文件" ></el-table-column>
      <el-table-column prop="statuz" label="状态" ></el-table-column>
       <el-table-column fixed="right" label="操作" width="300px">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="edit(scope.row.kid)">
            <i class="el-icon-edit"></i>
          </el-button>
           <el-button size="small"  @click="del(scope.row.kid)">
            <i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
     <page
      :page="formInline.page"
      :limit="formInline.limit"
      :total="formInline.total"
      @change="changePage"
    ></page>
    <Add ref="Add" @refresh="fetch"></Add>
    <Edit ref="Edit" @refresh="fetch"></Edit>
  </Main>
</template>

<script>
import Main from "@/components/basic-container/main";
import { baseUrl } from "@/config/env";
import page from "@/components/basic-page/page";
import Add from "./add";
import Edit from "./edit";

const urls = {
  delete: baseUrl + "/zweixin/article/delete",
  list: baseUrl + "/zweixin/article/list"
};
export default {
  methods: {
    add: function() {
      this.$refs.Add.show();
    },
    edit: function(kid) {
      this.$refs.Edit.show(kid);
    },
     del: function(kid) {
      this.$confirm("确认要删除该文章吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        axios.get(urls.delete, { params: { kid: kid } }).then(res => {
          this.$message(res.data.result);
          this.fetch();
        });
      });
    },
    sortChange(column, prop, order) {
      this.formInline.sorter = {
        field: column.prop,
        order: column.order
      };
      this.fetch();
    },
    changePage(val) {
      this.formInline.page = val;
      this.fetch();
    },
    fetch(kid) {
      axios.get(urls.list).then(res => {
        const data = res.data.result;
        this.tableData = data.list;
        this.formInline.page = data.pageNumber;
        this.formInline.limit = data.pageSize;
        this.formInline.total = data.totalRow;
      });
    }
  },
  mounted() {
    this.fetch();
  },
  data() {
    return {
      formInline: {
        name: null,
        code: null,
        limit: 10,
        page: 1,
        total: 0,
        sorter: {}
      },
      tableData: []
    };
  },
  components: {
    Add,
    page,
    Edit,
    Main
  }
};
</script>